package com.riven.bilibilidemo.fragment.discover;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.BaseActivity;
import com.riven.bilibilidemo.adapter.MyRvTopicCenterAdapter;
import com.riven.bilibilidemo.bean.TopicCenterBean;
import com.riven.bilibilidemo.interfaces.RequestJson2BeanCallBack;
import com.riven.bilibilidemo.utils.Api;
import com.riven.bilibilidemo.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 话题中心 Activity
 */
public class TopicCenterActivity extends BaseActivity {

    //注解得到控件对象
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.recycle)
    RecyclerView mRecycle;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private Handler mHandler = new Handler();

    //声明活动中心数据储存集合对象
    private List<TopicCenterBean.ListBean> topicList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置toolBar
        initToolbar();
        
        setSwipeRefresh();
    }

    private void setSwipeRefresh() {
        //设置刷新旋转圆圈颜色
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        //设置刷新监听
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000);
            }
        });

        loadData();
    }

    private void initToolbar() {
        mToolbar.setTitle("话题中心");
        mToolbar.setTitleTextColor(Color.WHITE);
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_topic_center);

    }

    @Override
    protected void initData() {
        //网络请求得到数据并加载到页面
        loadData();
    }

    @Override
    protected void initViews() {

    }

    //请求数据方法
    private void loadData() {
        HttpUtil.newInstance().getRequest(Api.TOPIC_CENTER_URL, TopicCenterBean.class, new RequestJson2BeanCallBack<TopicCenterBean>() {
            @Override
            public void onSucces(TopicCenterBean topicCenterBean) {
                topicList.clear();
                topicList = topicCenterBean.getList();
                LinearLayoutManager layoutManager = new LinearLayoutManager(TopicCenterActivity.this, LinearLayout.VERTICAL, false);
                mRecycle.setLayoutManager(layoutManager);
                MyRvTopicCenterAdapter adapter = new MyRvTopicCenterAdapter(TopicCenterActivity.this, topicList);
                mRecycle.setAdapter(adapter);
            }

            @Override
            public void onFail(String error) {

            }
        });
    }

}
